﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HandyControl.Controls;

namespace weighing
{
    /// <summary>
    /// 过磅设置 的交互逻辑
    /// </summary>
    public partial class WeighingConfigWindow : GlowWindow
    {
        #region variables
        private bool isSetValue = false;

        #endregion

        public WeighingConfigWindow()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);
        }

        #region window

        private void GlowWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void GlowWindow_ContentRendered(object sender, EventArgs e)
        {
            BindVale();
        }
        private void GlowWindow_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
        }
        #endregion

        #region set default config

        private void BindVale()
        {
            isSetValue = true;

            if (App.Config.WeighingConfig.WeighingMode == WeighingMode.Simple)
            {
                this.simpleModeRB.IsChecked = true;
            }
            else if (App.Config.WeighingConfig.WeighingMode == WeighingMode.Standard)
            {
                this.standardModeRb.IsChecked = true;
            }

            this.AutoRefreshTb.Text = App.Config.SystemConfig.DataRefreshInterval.ToString();

            isSetValue = false;
        }
        #endregion

        #region weighing mode 称重模式


        private void SimpleModeRB_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetValue || !IsLoaded) return;
            App.Config.WeighingConfig.WeighingMode = WeighingMode.Simple;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingWeighingModeChanged?.Invoke(WeighingMode.Simple);
        }

        private void StandardModeRb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetValue || !IsLoaded) return;
            App.Config.WeighingConfig.WeighingMode = WeighingMode.Standard;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingWeighingModeChanged?.Invoke(WeighingMode.Standard);

        }
        #endregion

        #region 自动刷新数据时间
        private void SaveAutoRefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            if (isSetValue || !IsLoaded) return;
            var temp = this.AutoRefreshTb.Text;
            if (string.IsNullOrEmpty(temp))
            {
                Growl.Warning("自动刷新数据时间填写错误");
                this.AutoRefreshTb.Text = "0";
                return;
            }
            int t = -1;
            try
            {
                t = Convert.ToInt32(temp);
            }
            catch (Exception ex)
            {
                Growl.Warning("自动刷新数据时间填写错误:"+ ex.Message);
            }
            App.Config.SystemConfig.DataRefreshInterval = t;
            CommonFunction.UpdateAppConfig(nameof(SystemConfig));            
        }
        #endregion

    }
}
